package com.business_clean.ui.fragment.todo;

import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.lifecycle.Observer;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.blankj.utilcode.util.SizeUtils;
import com.business_clean.R;
import com.business_clean.app.App;
import com.business_clean.app.base.BaseFragment;
import com.business_clean.app.uitl.ActivityForwardUtil;
import com.business_clean.app.uitl.CheckListDataUtil;
import com.business_clean.app.weight.EqualSpacingItemDecoration;
import com.business_clean.data.mode.todo.TodoList;
import com.business_clean.databinding.FragmentTodoItemBinding;
import com.business_clean.ui.activity.custom.AddProjectActivity;
import com.business_clean.ui.activity.depart.QuickDepartActivity;
import com.business_clean.ui.activity.todo.TodoDetailActivity;
import com.business_clean.ui.adapter.todo.TodoAdapter;
import com.business_clean.viewmodel.request.TodoViewModel;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import static com.business_clean.app.config.Constant.TYPE_INIT_DATA;
import static com.business_clean.app.config.Constant.TYPE_INIT_LOAD_MORE;

public class TodoItemFragment extends BaseFragment<TodoViewModel, FragmentTodoItemBinding> {

    private TodoAdapter mAdapter = null;

    private int page = 1;
    private int requestType;
    private int type = 1;


    public TodoItemFragment() {
    }

    public TodoItemFragment(int type) {
        this.type = type;
    }

    @Override
    public void initView(@Nullable Bundle savedInstanceState) {
        if (type == 2) {
            mAdapter = new TodoAdapter(true);
        } else {
            mAdapter = new TodoAdapter(false);
        }
        getMDatabind().list.recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        getMDatabind().list.recyclerView.setAdapter(mAdapter);
        getMDatabind().list.recyclerView.addItemDecoration(new EqualSpacingItemDecoration(SizeUtils.dp2px(10)));
        getMDatabind().list.refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestMore();
            }

            @Override
            public void onRefresh(@NonNull @NotNull RefreshLayout refreshLayout) {
                requestOne();
            }
        });
    }

    @Override
    public void onLoadRetry() {
        lazyLoadData();
    }

    @Override
    public void lazyLoadData() {
        requestOne();
    }

    @Override
    public void initObserver() {
        //监听创建成员成功
        App.appViewModelInstance.getRefreshMember().observe(this, new Observer<Boolean>() {
            @Override
            public void onChanged(Boolean aBoolean) {
                requestOne();
            }
        });
    }

    @Override
    public void onRequestSuccess() {
        mViewModel.getTodoList().observe(this, new Observer<TodoList>() {
            @Override
            public void onChanged(TodoList todoList) {
                getMDatabind().list.refreshLayout.finishRefresh();
                switch (requestType) {
                    case TYPE_INIT_DATA:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkInitData(todoList.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.setList(todoList.getList());
                        }
                        break;
                    case TYPE_INIT_LOAD_MORE:
                        // 根据数据来决定要显示哪一个状态的View
                        if (CheckListDataUtil.checkLoadMoreData(todoList.getList(), mAdapter, getMDatabind().list.refreshLayout)) {
                            mAdapter.addData(todoList.getList());
                        }
                        break;
                }
            }
        });
        mViewModel.getAgree().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                App.appViewModelInstance.getRefreshTodo().setValue(true);
                requestOne();
            }
        });

        mViewModel.getCancel().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                App.appViewModelInstance.getRefreshTodo().setValue(true);
                requestOne();
            }
        });

        mViewModel.getReject().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                App.appViewModelInstance.getRefreshTodo().setValue(true);
                requestOne();
            }
        });

        mViewModel.getDel().observe(this, new Observer<Object>() {
            @Override
            public void onChanged(Object o) {
                App.appViewModelInstance.getRefreshTodo().setValue(true);
                requestOne();
            }
        });
    }

    @Override
    public void onBindViewClick() {
        //item 字item 点击
        mAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull @NotNull BaseQuickAdapter adapter, @NonNull @NotNull View view, int position) {

                switch (view.getId()) {
                    case R.id.but_item_refuse://拒绝
                        mViewModel.requestTodoReject(mAdapter.getData().get(position).getApplication_no());
                        break;
                    case R.id.but_item_agree://同意
                        //这里判断一下，如果状态是重新提交，那么就跳转详情页 要先处理一下是离职审批还是入职审批
                        if ("1".equals(mAdapter.getData().get(position).getApplication_type())) {//1入职审批 2离职审批
                            if ("0".equals(mAdapter.getData().get(position).getApplication_status())) {
                                Bundle bundle = new Bundle();//这里还是新建
                                bundle.putInt("type", 1);
                                bundle.putString("application_no", "" + mAdapter.getData().get(position).getApplication_no());
                                ActivityForwardUtil.startActivity(AddProjectActivity.class, bundle);
                            } else if ("3".equals(mAdapter.getData().get(position).getApplication_status()) || "4".equals(mAdapter.getData().get(position).getApplication_status())) {
                                Bundle bundle = new Bundle();
                                bundle.putInt("type", 2);//走新建的流程
                                bundle.putString("application_no", "" + mAdapter.getData().get(position).getApplication_no());
                                ActivityForwardUtil.startActivity(AddProjectActivity.class, bundle);
                            } else {
                                mViewModel.requestTodoAgree(mAdapter.getData().get(position).getApplication_no());
                            }
                        } else {//离职审批
                            if ("3".equals(mAdapter.getData().get(position).getApplication_status()) || "4".equals(mAdapter.getData().get(position).getApplication_status())) {//判断状态是重新提交还是
                                Bundle bundle = new Bundle();
                                bundle.putString("application_no", mAdapter.getData().get(position).getApplication_no());
                                ActivityForwardUtil.startActivity(QuickDepartActivity.class, bundle);
                            } else {
                                mViewModel.requestTodoAgree(mAdapter.getData().get(position).getApplication_no());
                            }
                        }
                        break;
                    case R.id.but_item_withdraw://撤回
                        mViewModel.requestTodoCancel(mAdapter.getData().get(position).getApplication_no());
                        break;
                    case R.id.but_item_del://删除
                        mViewModel.requestDel(mAdapter.getData().get(position).getApplication_no());
                        break;
                }
            }
        });
        //item点击
        mAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                Bundle bundle = new Bundle();
                bundle.putString("application_type", "" + mAdapter.getData().get(position).getApplication_type());
                bundle.putString("application_no", "" + mAdapter.getData().get(position).getApplication_no());
                ActivityForwardUtil.startActivity(TodoDetailActivity.class, bundle);
            }
        });
    }

    private void requestOne() {
        requestType = TYPE_INIT_DATA;
        page = 1;
        mViewModel.requestTodoList(page, type);
    }

    private void requestMore() {
        requestType = TYPE_INIT_LOAD_MORE;
        page++;
        mViewModel.requestTodoList(page, type);
    }
}
